using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static UnityEngine.EventSystems.EventTrigger;

public class IfItem : MonoBehaviour
{
    public WhatToDoDropDown m_WhatToDoPrefab;
    public Text m_AndPrefab;
    List<WhatToDoDropDown> m_WhatToDo = new List<WhatToDoDropDown>();
    List<Text> m_Ands = new List<Text>();
    public GameObject m_WhatToDoParent;
    public GameObject m_DetailParent;
    WhatToDoDropDown m_SelectedWhatToDo;
    public Color m_UnselectedColor;
    public Color m_SelectedColor;

    public ShipEventTrigger.TriggerType m_SelectedTriggerType;

    public ControleComponentParameter m_ControleComponentPerfab;
    public GameObject m_DetailParameter;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    public void Init(List<ShipEventTrigger> tirgger)
    {
        foreach(var i in tirgger)
        {
            AddWhatToDo(i);
        }
    }
    public void OnAddAndClicked()
    {
        AddWhatToDo(null);
    }
    public void AddWhatToDo(ShipEventTrigger trigger)
    {
        var whatToDo = Instantiate(m_WhatToDoPrefab);
        if (m_WhatToDo.Count == 0)
            m_SelectedWhatToDo = whatToDo;
        m_WhatToDo.Add(whatToDo);

        if(trigger != null)
        {
            whatToDo.SetTrigger( trigger);
            OnWhatToDoValueChanged(whatToDo, (int)trigger.m_TriggerType);
        }
        whatToDo.m_OnSelectedCallback = OnSelectedWhatToDo;
        whatToDo.m_OnValueChangedCallback = OnWhatToDoValueChanged;
        whatToDo.transform.SetParent(m_WhatToDoParent.transform);
        whatToDo.transform.SetSiblingIndex(Mathf.Max(m_WhatToDoParent.transform.childCount - 3, 0));
        var and = Instantiate(m_AndPrefab);
        and.transform.SetParent(m_WhatToDoParent.transform);
        and.transform.SetSiblingIndex(Mathf.Max(m_WhatToDoParent.transform.childCount - 3, 0));
        m_Ands.Add(and);
    }

    public void OnSelectedWhatToDo(WhatToDoDropDown dropdown)
    {
        if (dropdown == m_SelectedWhatToDo)
            return;
        if(m_DetailParameter != null)
            Destroy(m_DetailParameter);
        if (m_SelectedWhatToDo != null)
        {
            m_SelectedWhatToDo.GetComponent<Image>().color = m_UnselectedColor;
        }
        m_SelectedWhatToDo = dropdown;

        if (m_SelectedWhatToDo != null)
        {
            m_SelectedWhatToDo.GetComponent<Image>().color = m_SelectedColor;
            var trigger = m_SelectedWhatToDo.GetTrigger();
            if(trigger != null)
                OnWhatToDoValueChanged(dropdown, (int)trigger.m_TriggerType);
        }
    }
    public List<ShipEventTrigger> GetShipEventTriggers()
    {
        List<ShipEventTrigger> triggers = new List<ShipEventTrigger>();
        foreach(var i in m_WhatToDo)
        {
            if(null != i && null != i.GetTrigger())
                triggers.Add(i.GetTrigger());
        }
        return triggers;
    }

    public void OnWhatToDoValueChanged(WhatToDoDropDown dropdown, int value)
    {
        for(int i=0; i< m_DetailParent.transform.childCount; ++i)
        {
            var objTransform=m_DetailParent.transform.GetChild(i);
            Destroy(objTransform.gameObject);
        }
        m_DetailParent.SetActive(true);
        switch (value)
        {
            case 0:
                m_DetailParent.SetActive(false);
                break;
            case 1:
                m_SelectedTriggerType = ShipEventTrigger.TriggerType.ControleSystem;
                var controleC = Instantiate(m_ControleComponentPerfab);
                controleC.transform.SetParent(m_DetailParent.transform);
                controleC.Init(m_SelectedWhatToDo);
                m_DetailParameter = controleC.gameObject;
                break;
            case 2:
                m_SelectedTriggerType = ShipEventTrigger.TriggerType.PartStateChenged;
                break;
        }
        LayoutRebuilder.ForceRebuildLayoutImmediate(transform.parent.gameObject.GetComponent<RectTransform>());
        LayoutRebuilder.ForceRebuildLayoutImmediate(gameObject.GetComponent<RectTransform>());
    }
}
